package com.guoxue.action.front;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.guoxue.domain.AdvertisementVo;
import com.guoxue.service.IAdvertisementService;
import com.guoxue.util.LoggerUtil;
import com.guoxue.util.OSSUtil;

@Controller
@RequestMapping(value="/advertisement")
public class AdvertisementAction extends BaseAction{
	
	@Resource
	private IAdvertisementService advertisementService;
	@Resource
	private OSSUtil oss;
	
	@ResponseBody
	@RequestMapping("/getAdvertisementList")
	public JSON getAdvertisementList(HttpServletRequest req, HttpServletResponse resp,Integer uid) {
		try {
			if (uid == null) {
				return Message(ERROR, "未传用户id", null);
			}
			List<AdvertisementVo> advertisementList = advertisementService.getNotExpiredAdvertisementList();
			if(advertisementList.size() > 0){
				for(AdvertisementVo a:advertisementList){
					a.setPic(isEmpty(a.getPic())?null:oss.getUrl(bucket, a.getPic(),  new Date(new Date().getTime() + 3600 * 1000 * 12)));
				}
				if(advertisementList.size() > 8){
					advertisementList.subList(0, 8);
				}
				return Message(SUCCESS, "获取数据成功", advertisementList);
			}else{
				return Message(SUCCESS, "获取数据成功", null);
			}
		} catch (Exception e) {
			return LoggerUtil.logInfo(e, "获取数据失败");
		}
	}
	
}
